package cn.lyd.pet.service.impl;

import cn.lyd.base.enm.PetStateEnum;
import cn.lyd.base.until.BaiduAuditUtils;
import cn.lyd.base.until.BussinessException;
import cn.lyd.base.until.ThreadLocalUtil;
import cn.lyd.org.domain.Shop;
import cn.lyd.pet.domain.Pet;
import cn.lyd.pet.domain.PetDetail;
import cn.lyd.pet.domain.PetOnlineAuditLog;
import cn.lyd.pet.mapper.PetDetailMapper;
import cn.lyd.pet.mapper.PetMapper;
import cn.lyd.pet.mapper.PetOnlineAuditLogMapper;
import cn.lyd.pet.service.IPetService;
import cn.lyd.base.service.impl.BaseServiceImpl;
import cn.lyd.user.domain.Logininfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类     非店铺人员无权添加和修改宠物
 * </p>
 *
 * @author lyd
 * @since 2023-01-08
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper   logMapper;

    /**
     * 重写宠物添加：添加宠物明细
     * @param pet
     */
    @Override
    public void add(Pet pet) {
        //1.参数校验

        //2.添加宠物---自增长id 会返回id给当前对象
        Pet addPet = new Pet();
        BeanUtils.copyProperties(pet,addPet);
        addPet.setDetail(pet.getDetail());
        addPet.setState(PetStateEnum.OFFSALE.getCode());//0默认下架 1上架
        addPet.setCreatetime(new Date());
        //通过当前登录人获取店铺id--如果为空=平台人员
        Logininfo logininfo = ThreadLocalUtil.getUserInfo().getLogininfo();
        if (logininfo.getShop()==null){//非店铺人员
           throw new BussinessException("非店铺人员无权添加！");
        }else{//平台人员
            addPet.setShop_id(logininfo.getShop().getId());
            super.add(addPet);
        }

        //3.添加宠物明细
        PetDetail petDetail = new PetDetail();
        petDetail.setPet_id(addPet.getId());
        petDetail.setAdoptNotice(addPet.getDetail().getAdoptNotice());
        petDetail.setIntro(addPet.getDetail().getIntro());
        petDetailMapper.add(petDetail);

    }


    /**
     * 重写宠物删除：删除宠物明细
     * @param id
     */
    @Override
    public void delete(Long id) {

        //1.删除宠物明细
        petDetailMapper.deleteByPetId(id);

        //2.删除宠物
        super.delete(id);
    }


    /**
     * 重写宠物修改：修改宠物明细
     * @param pet
     */
    @Override
    public void update(Pet pet) {
        //1.参数校验

        //2.修改宠物明细
        Logininfo logininfo = ThreadLocalUtil.getUserInfo().getLogininfo();
        if (logininfo.getShop()==null){
            throw new BussinessException("非店铺人员无权修改！");
        }else {
            PetDetail updatePetDetail = new PetDetail();
            updatePetDetail.setId(pet.getDetail().getId());
            updatePetDetail.setPet_id(pet.getId());
            updatePetDetail.setIntro(pet.getDetail().getIntro());
            updatePetDetail.setAdoptNotice(pet.getDetail().getAdoptNotice());
            petDetailMapper.update(updatePetDetail);
            //3.修改宠物
            super.update(pet);
        }

    }


    /**
     * 通过宠物id查询
     * @param id     宠物id
     * @return
     */
    @Override
    public Pet findDetailById(Long id) {
        return petMapper.findDetailById(id);
    }


    /**
     * 宠物上架
     * @param ids
     */
    @Override
    public void onsale(List<Long> ids) {
        //遍历前端传来的宠物id数组-请求上架
        for (Long id : ids){
            //1.获取每一个宠物
            Pet pet = petMapper.findById(id);

            //2.百度ai判断是否违规
            StringBuffer sb = new StringBuffer();
            if(!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append("宠物名称审核失败,宠物名称不合法："+pet.getName());
            }
//            for (String filePath:pet.getResources().split(",")) {
//                if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+filePath)){
//                    sb.append("宠物图片审核失败,宠物图片不合法:"+"http://123.207.27.208"+filePath);
//                }
//            }

            //3.存在违规就执行---添加审核失败的日志
            if (sb.length()>0){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setAuditId(351L);//tom
                log.setAuditTime(new Date());
                log.setNote("审核未通过！存在非法信息！");
                logMapper.add(log);
                //继续遍历
                continue;
            }
            //不违规，上架
            petMapper.onsale(id);

        }

    }

    /**
     * 宠物下架
     * @param ids
     */
    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    /**
     *  前端展示宠物详情
     * @param id
     * @return
     */
    @Override
    public Pet findShowById(Long id) {
        return petMapper.findShowById(id);
    }


}
